/* 获取浏览器Cookie的值 */
const cookie = name => `; ${document.cookie}`.split(`; ${name}=`).pop().split(';').shift();

/* 颜色RGB转十六进制 */
const rgbToHex = (r, g, b) => "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);

/* 复制到剪贴板 */
const copyToClipboard = (text) => navigator.clipboard.writeText(text);

/* 检查日期是否合法 */
const isDateValid = (...val) => !Number.isNaN(new Date(...val).valueOf());

/* 查找日期位于一年中的第几天 */
const dayOfYear = (date) => Math.floor((date - new Date(date.getFullYear(), 0, 0)) / 1000 / 60 / 60 / 24);

/* 英文字符串首字母大写 */
const capitalize = str => str.charAt(0).toUpperCase() + str.slice(1);

/* 计算2个日期之间相差多少天 */
const dayDif = (date1, date2) => Math.ceil(Math.abs(date1.getTime() - date2.getTime()) / 86400000);

/* 清楚全部Cookie */
const clearCookies = document.cookie.split(';').forEach(cookie => document.cookie = cookie.replace(/^ +/, '').replace(/=.*/, `=;expires=${new Date(0).toUTCString()};path=/`));

/* 生成随机十六进制颜色 */
const randomHex = () => `#${Math.floor(Math.random() * 0xffffff).toString(16).padEnd(6, "0")}`;

/* 数组去重 */
const removeDuplicats = (arr) => [...new Set(arr)];

/* 从URL获取查询参数 */
const getParameters = (URL) => {
  URL = JSON.parse(
    '{"' +
      decodeURI(URL.split("?")[1])
        .replace(/"/g, '\\"')
        .replace(/&/g, '","')
        .replace(/=/g, '":"') +
    '"}'
  );
  return JSON.stringify(URL);
};
const getParameters1 = () => {
  return Object.fromEntries(new URLSearchParams(window.location.search));
}

/* 时间处理 */
const timeFromDate = date => date.toTimeString().slice(0,8);

/* 校验数字是奇数还是偶数 */
const isEven = num => num % 2 === 0;

/* 求数字的平均值 */
const average = (...args) => args.reduce((a, b) => a + b) / args.length;

/* 回到顶部 */
const goToTop = () => window.scrollTo(0, 0);

/* 翻转字符串 */
const reverse = str => str.split('').reverse().join('');

/* 校验数组是否为空 */
const isNotEmpty = arr => Array.isArray(arr) && arr.length > 0;

/* 获取用户选择的文本 */
const getSelectedText = () => window.getSelection().toString();

/* 打乱数组 */
const shuffleArray = (arr) => arr.sort(() => 0.5 - Math.random());

/* 检查用户的设备是否处于暗模式 */
const isDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches
